import router from './router';
import NProgress from 'nprogress';
import axios from 'axios';
// 存放放行路由
let pathMap = new Map();
pathMap.set('/login',true);
pathMap.set('/register',true);
pathMap.set('/forget',true);
pathMap.set('/password/reset',true);
pathMap.set('/share/main',true);
pathMap.set('/main/cloud-disk/recycle',false);
pathMap.set('/main/cloud-disk/share',false);
pathMap.set('/main/cloud-disk/files',false);
// 判断路由是否放行
function releasePath(path){
    return pathMap.get(path);
}

router.beforeEach(async(to,from,next) => {

    
    NProgress.start();
    if (releasePath(to.path)
        ||to.path.startsWith('/share/')
        ||to.path.startsWith('/share/main/')) {
        // 放行页
        console.log(to);
        next();
        NProgress.done();
    } else {
        // 检查登录
        await axios.post('http://127.0.0.1:8080/user/check',{

        }).then(response => {
            let result = response.data;
            // 判断返回码
            if (result.code === 200){
                // 已登录 ，放行
                next();
            }else{
                
                // 未登录，跳转至登录页
                next("/login");
            }
        }).catch(err => {
            next('/login');
        })
        NProgress.done();
    }

})

